package com.yanqu.road.server.manger.system;

import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.logic.bussiness.servercenter.PlatformGoodsBussiness;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.PlatformGoodsModule;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.TempCommonMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PlatformGoodsMgr extends TempMgr {

    private static Map<Integer, GoodsInfo> goodsInfoMap;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean reloadData() {
        reloadPlatformData();
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    public static void reloadPlatformData(){
        int channelId = 0;
        ServerInfo serverInfo = ServerListMgr.getServerInfo(GameServer.getInstance().getServerId());
        if(null != serverInfo){
            channelId = serverInfo.getChannelId();
        }
        Map<Integer, GoodsInfo> tempGoodsInfoMap = PlatformGoodsBussiness.getGoodsInfoMap(channelId);
        TempCommonMgr.initGoodsInfoMap(tempGoodsInfoMap);
        //重新加载语言
        ServerLanguageMgr.reloadLanguage();
        goodsInfoMap = tempGoodsInfoMap;
        notifyPlayerPlatformData();
    }

    private static void notifyPlayerPlatformData(){
        List<GamePlayer> playerList = GamePlayerMgr.getCopyPlayerList();
        for(GamePlayer player : playerList){
            player.getModule(PlatformGoodsModule.class).syncPlatformData();
        }
    }
    public static GoodsInfo getGoodsInfo(int goodsId){
        return goodsInfoMap.get(goodsId);
    }

    public static List<GoodsInfo> getGoodsInfoList(){
        return new ArrayList<>(goodsInfoMap.values());
    }
}
